﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Citi.Helpers;
using Citi.RoutineBase;
using Citi.SecurityHelper;

namespace Citi.MISGLV.SchedulerUI
{
    public class Extraction : IExecutableRoutine
    {
        public void Rollback(ref ICurrentAction currentAction)
        {

        }

        public void Start(DateTime scheduledDate, ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {
            string url = @"https://globalconsumer.collaborationtools.consumer.citigroup.net/sites/BR_Cards/FPA_BR/Reports_Decks/Published%20Reports%20%20Decks/03.%20Consumer/01.%20MIS_GLV/";
            string fileName = "MIS-GLV.zip";
            string fileUrl = string.Concat(url, fileName);

            var saveDirectory = @"\\gruvnascti0016.wlb.lac.nsroot.net\DATA_GRP1\FTOOLS\Apps\MISGLV\ExtractedFiles\";
            var localPath = Path.Combine(saveDirectory, fileName);

            if (File.Exists(localPath)) File.Delete(localPath);

            currentAction.Description = string.Format("Downloading file from {0} to {1}", fileUrl, localPath);
            SecurityDownload.DownloadRemoteFile(fileUrl, localPath, parameters["RobotUsername"].ToString().Split('\\').Last(), parameters["Proxy"].ToString());

            currentAction.Description = "File Downloaded";

            currentAction.Description = "Decompressing file";
            FileHelper.Decompress(localPath, saveDirectory);
            currentAction.Description = "File Decompressed";
        }

        public void Stop(ref ICurrentAction currentAction)
        {

        }
    }
}
